package com.dog.basics.api.controller.mtkj;


import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.api.mtkj.*;
import com.dog.basics.bean.model.mtkj.MtOrder;
import com.dog.basics.bean.vo.api.mtkj.ApiOrderPrepayVo;
import com.dog.basics.bean.vo.api.mtkj.ApiShopOrderVo;
import com.dog.basics.bean.vo.api.mtkj.ApiShopOrderXqVo;
import com.dog.basics.bean.vo.api.mtkj.ApiSjDataVo;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.common.WeChatMiniService;
import com.dog.basics.service.mtkj.IMtOrderService;
import com.dog.basics.service.sys.MobileService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.net.SocketException;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author wph
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/api/order")
@Api(value = "商城订单模块", tags = SwaggerConstants.API_ALL)
@AllArgsConstructor
public class MtOrderController {
    private final IMtOrderService mtOrderService;
    private final WeChatMiniService weChatMiniService;
    private final MobileService mobileService;
    /**
     * @Description: 获取我的订单列表
     * @Param: CashWithdrawalAddDto
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "cs", notes = "cs", tags = SwaggerConstants.API_SHOPPINGORDE)
    @GetMapping("/cs")
    @Inner
    @ApiIgnore
    public R<Boolean> cs(String phone) {
        return R.ok(mobileService.sendOrderSms(phone, 1L));
    }
    /**
     * @Description: 获取我的订单列表
     * @Param: CashWithdrawalAddDto
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "获取我的订单列表", notes = "获取我的订单列表", tags = SwaggerConstants.API_SHOPPINGORDE)
    @GetMapping
    public R<IPage<ApiShopOrderVo>> getOrderInfo(PageParam page, ApiShopOrderFromDto obj) {
        return R.ok(mtOrderService.getApiPageList(page, obj));
    }

    /**
     * @Description: 获取商家的经营数据
     * @Param: CashWithdrawalAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "获取商家的经营数据", notes = "获取商家的经营数据", tags = SwaggerConstants.API_SHOPPINGORDESJ)
    @GetMapping("/sjJyObj")
    public R<ApiSjDataVo> getSjJyObj(ApiSjDataFromDto obj) {
        return R.ok(mtOrderService.getApiSjJyObj(obj));
    }

    /**
     * @Description: 获取商家的订单列表
     * @Param: CashWithdrawalAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "获取商家的订单列表", notes = "获取商家的订单列表", tags = SwaggerConstants.API_SHOPPINGORDESJ)
    @GetMapping("/sjOrderList")
    public R<IPage<ApiShopOrderVo>> getSjOrderInfo(PageParam page, ApiShopOrderFromDto obj) {
        return R.ok(mtOrderService.getApiSjPageList(page, obj));
    }

    /**
     * @Description: 订单详情通过订单编号
     * @Param: CashWithdrawalAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "订单详情通过订单编号", notes = "订单详情通过订单编号", tags = {SwaggerConstants.API_SHOPPINGORDE, SwaggerConstants.API_SHOPPINGORDESJ})
    @GetMapping("/orderNo/{orderNo}")
    @ApiImplicitParam(name = "orderNo", value = "订单编号", required = true, dataType = "String")
    public R<ApiShopOrderXqVo> getApiObjXq(@PathVariable("orderNo") String orderNo) {
        return R.ok(mtOrderService.getApiObjByOrderNoXq(orderNo));
    }
    /**
     * @Description: 订单详情
     * @Param: CashWithdrawalAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "订单详情", notes = "订单详情", tags = {SwaggerConstants.API_SHOPPINGORDE, SwaggerConstants.API_SHOPPINGORDESJ})
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long")
    public R<ApiShopOrderXqVo> getApiObjXq(@PathVariable("id") Long id) {
        return R.ok(mtOrderService.getApiObjXq(id));
    }

    /**
     * @Description: 是否在配送范围内
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "是否在配送范围内", notes = "是否在配送范围内", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PutMapping("/sfSend")
    public R<Boolean> sfSend(@Valid @RequestBody ApiShopOrderSendFromDto obj) {
        return R.ok(mtOrderService.sfSend(obj));

    }

    /**
     * @Description: 商品下单
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "商品下单", notes = "商品下单", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PostMapping("/saveObj")
    public R saveObj(@Valid @RequestBody ApiShopOrderAddDto obj) {
        //下单
        ApiOrderPrepayVo orderPrepayVo = mtOrderService.saveObj(obj);
        if (orderPrepayVo.getHnrbMoney().compareTo(BigDecimal.ZERO) <= 0) {
            // 更新订单状态
            Validator.validateTrue(mtOrderService.paymentResultVerification(orderPrepayVo.getOrderNo()), "商品下单失败!");
            return R.ok(orderPrepayVo.getOrderId());
        } else {
            //微信小程序预支付
            return weChatMiniService.callOrderPrepayment(orderPrepayVo);
        }


    }

    /**
     * @Description: 购物车下单
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "购物车下单", notes = "购物车下单", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PostMapping("/saveBasketOrder")
    public R saveBasketOrder(@Valid @RequestBody ApiOrderBasketAddDto obj) {
        //下单
        ApiOrderPrepayVo orderPrepayVo = mtOrderService.saveBasketObj(obj);
        if (orderPrepayVo.getHnrbMoney().compareTo(BigDecimal.ZERO) <= 0) {
            // 更新订单状态
            Validator.validateTrue(mtOrderService.paymentResultVerification(orderPrepayVo.getOrderNo()), "购物车下单失败!");
            return R.ok(orderPrepayVo.getOrderId());
        }else{
            //微信小程序预支付
            return weChatMiniService.callOrderPrepayment(orderPrepayVo);
        }


    }

    /**
     * 小程序微信 Order订单 - 支付回调
     *
     * @param
     * @return
     */
    @Inner
    @ApiIgnore
    @PostMapping("/wxPay/minicallbackOrder")
    public String wxMiniPayLowerOrderCallback(@RequestBody String xmlData) {
        if (mtOrderService.wxMiniPayLowerOrderCallback(xmlData)) {
            return WxPayNotifyResponse.success("成功");
        }
        return WxPayNotifyResponse.fail("失败");
    }

    /**
     * @Description: 重新支付
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "重新支付", notes = "重新支付", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PostMapping("/rePayOrder/{orderId}")
    public R basketOrder(@PathVariable Long orderId) throws SocketException {
        //下单
        MtOrder mtOrder = mtOrderService.getById(orderId);
        Validator.validateNotNull(mtOrder, "订单不存在!");
        Validator.validateEqual(mtOrder.getRefundStatus(), 0, "只有待支付的订单才能重新支付");
        if (mtOrder.getPayMoney().compareTo(BigDecimal.ZERO) <= 0) {
            // 更新订单状态
            Validator.validateTrue(mtOrderService.paymentResultVerification(mtOrder.getOutTradeNo()), "重新支付失败!");
            return R.ok();
        } else {
            //微信小程序预支付
            return weChatMiniService.reShoppPayOrder(orderId);
        }


    }

    /**
     * @Description: 申请退款
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "申请退款", notes = "申请退款", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PutMapping("/applyreFund")
    public R<Boolean> applyreFund(@Valid @RequestBody ApiOrderRefundAddDto obj) {
        return R.ok(mtOrderService.applyreFund(obj));
    }

    /**
     * 订单发货
     *
     * @param orderId
     * @return
     * @author fengjk
     */
    @ApiOperation(value = "订单发货", notes = "订单发货", tags = SwaggerConstants.API_SHOPPINGORDESJ)
    @PutMapping("sendOrder/{orderId}")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "Long")
    public R sendOrder(@PathVariable("orderId") Long orderId) {
        return R.ok(mtOrderService.sendOrder(orderId));
    }

    /**
     * @Description: 确认收货
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "确认收货", notes = "确认收货", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PutMapping("/conOrder/{id}")
    @ApiImplicitParam(name = "id", value = "订单ID", required = true, dataType = "Long")
    public R<Boolean> conOrder(@PathVariable("id") Long id) {
        return R.ok(mtOrderService.conOrder(id));
    }

    /**
     * @Description: 取消订单
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "取消订单", notes = "取消订单", tags = SwaggerConstants.API_SHOPPINGORDE)
    @PutMapping("/deleteObj/{orderId}")
    public R<Boolean> deleteObj(@PathVariable Long orderId) {
        return R.ok(mtOrderService.deleteObj(orderId));
    }

    /**
     * 确认退款
     *
     * @param obj
     * @return
     * @author fengjk
     */
    @ApiOperation(value = "确认退款", notes = "确认退款", tags = SwaggerConstants.API_SHOPPINGORDESJ)
    @PutMapping("/editOrderInfoFund")
    public R<Boolean> editOrderInfoFund(@Valid @RequestBody ApiOrderRefundEditDto obj) {
        return R.ok(mtOrderService.editOrderInfoFund(obj));
    }

}
